package cn.edu.sgu.www.mhxysy.service.equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.equipment.ForgeBookCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.equipment.ForgeBookCategory;
import cn.edu.sgu.www.mhxysy.mapper.equipment.ForgeBookCategoryMapper;
import cn.edu.sgu.www.mhxysy.service.equipment.ForgeBookCategoryService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ForgeBookCategoryServiceImpl implements ForgeBookCategoryService {

    private final ForgeBookCategoryMapper forgeBookMapper;

    @Autowired
    public ForgeBookCategoryServiceImpl(ForgeBookCategoryMapper forgeBookMapper) {
        this.forgeBookMapper = forgeBookMapper;
    }

    @Override
    public void insert(ForgeBookCategoryDTO forgeBookCategoryDTO) {
        ForgeBookCategory forgeBookCategory = toEntity(forgeBookCategoryDTO);

        forgeBookCategory.setId(null);

        forgeBookMapper.insert(forgeBookCategory);
    }

    @Override
    public void updateById(ForgeBookCategoryDTO forgeBookCategoryDTO) {
        ObjectUtils.requireNonNull(forgeBookCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        ForgeBookCategory forgeBookCategory = toEntity(forgeBookCategoryDTO);

        forgeBookMapper.updateById(forgeBookCategory);
    }

    @Override
    public Page<ForgeBookCategory> selectByPage(Pager<ForgeBookCategory> pager) {
        Page<ForgeBookCategory> page = Pager.ofPage(pager);

        return forgeBookMapper.selectPage(page, null);
    }

    private ForgeBookCategory toEntity(ForgeBookCategoryDTO forgeBookCategoryDTO) {
        ForgeBookCategory forgeBookCategory = new ForgeBookCategory();

        forgeBookCategory.setId(forgeBookCategoryDTO.getId());
        forgeBookCategory.setName(forgeBookCategoryDTO.getName());
        forgeBookCategory.setEquipmentId(forgeBookCategoryDTO.getEquipmentId());

        return forgeBookCategory;
    }

}